package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseSearch;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVO;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "课程管理")
@RestController

@RequestMapping("/admin/edu/course")
public class AdminCourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    CourseDescriptionService courseDescriptionService;

    //点击发布按钮时
    @ApiOperation("根据课程id发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourseById(@PathVariable String id){
        boolean result = courseService.publishCourseById(id);
        if (result){
            return R.ok().message("发布成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id获取课程发布信息")
    @GetMapping("coursePublish/{id}")
    public R getCoursePublishVoById(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null){
            return R.ok().data("item",coursePublishVo);
        }else{
            return R.error().message("数据不存在");
        }
    }

    //4.课程列表信息
    @ApiOperation("分页课程列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R getList(@PathVariable Long pageNum,@PathVariable Long pageSize, CourseSearch courseSearch){
        Page<CourseVO> page =  courseService.getListByCondition(pageNum,pageSize,courseSearch);

        return R.ok().data("page",page);

    }


    //3.更新课程基本信息
    @PutMapping("updateCourseInfo/{courseId}")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable String courseId){
        //在课程的service层执行更新课程info的方法
        courseService.updateCourseInfo(courseInfoForm,courseId);
        return R.ok();
    }

    //2.根据课程id获得课程基本信息 回显数据
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseById(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);

        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        courseInfoForm.setDescription(courseDescription.getDescription());

        return R.ok().data("item",courseInfoForm);

    }


    //1.保存课程的基本信息，并返回课程id
    @ApiOperation("新增课程基本信息")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId).message("保存成功");
    }
}
